import { reactive } from 'vue'
import { ILoginEntity } from '@/types'
import { useUserAPI } from '@/server'
import { ObjectUtils, AutheUtils } from '@/utils'
import { useRouter } from 'vue-router'

export function useUserEmailLogin() {
    const { login: dologin } = useUserAPI();
    const router = useRouter();

    let entity = reactive({ email: "" });


    const login = function (entity: ILoginEntity): void {
        if (ObjectUtils.isEmpty(entity.email)) {
            alert("邮箱地址不能为空")
            return;
        }

        if (ObjectUtils.isEmpty(entity.vcode)) {
            alert("验证码不能为空")
            return;
        }

        dologin(entity).then(res => {
            const { data } = res;
            if (data.code == 200) {
                AutheUtils.setToken(data.token)
                router.push({name:"index"})
            } else {
                alert(data.msg)
            }
        })
    }
    return {
        entity,
        login
    }
}